package com.android.BluethootChess.rules;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Point;

import com.android.BluethootChess.model.GameBoardMatrix;
import com.android.BluethootChess.model.PieceColor;
import com.android.BluethootChess.util.Utils;

/**
 * PawnRules clasa care implementeaza interfata PieceRulesInterface si defineste
 * regulile pentru piesa pion.
 * 
 * @see PieceRule
 */
public class PawnRules extends PieceRule {

	public PawnRules(GameBoardMatrix matrix, PieceColor pieceColor) {
		super(matrix, pieceColor);
	}

	public List<Point> findAllAvaiblePositions() {
		List<Point> list = new ArrayList<Point>();
		if (pieceColor == PieceColor.BLACK) {
			findBlackPawnMoveLocations(list);
		}
		if (PieceColor.WHITE == pieceColor) {
			findWhitePawnMoveLocations(list);
		}

		return list;
	}

	//TODO refactorizeaza metodele astea.
	private void findWhitePawnMoveLocations(List<Point> list){
		int pieceLine = piecePosition.x;
		int pieceCol = piecePosition.y;

		if (pieceLine - 1 >= 0 && matrix[pieceLine - 1][pieceCol] == -1) {
			list.add(new Point(pieceLine - 1, pieceCol));
		}

		if (pieceLine - 1 >=0  && pieceCol + 1 <= matrix.length - 1
				&& (matrix[pieceLine - 1][pieceCol + 1] != -1)
				&& Utils.getPieceColor(matrix[pieceLine - 1][pieceCol + 1]) != pieceColor) {
			list.add(new Point(pieceLine - 1, pieceCol + 1));
		}

		if (pieceCol - 1 >= 0 && (pieceLine - 1 >=0 )
				&& (pieceCol - 1 >= 0)
				&& (matrix[pieceLine - 1][pieceCol - 1] != -1) ){
				if (Utils.getPieceColor(matrix[pieceLine - 1][pieceCol - 1]) != pieceColor) {
					list.add(new Point(pieceLine - 1, pieceCol - 1));
				}
		}
	}
	
	private void findBlackPawnMoveLocations(List<Point> list){
		int pieceLine = piecePosition.x;
		int pieceCol = piecePosition.y;

		if (pieceLine + 1 <= matrix.length - 1 && matrix[pieceLine + 1][pieceCol] == -1) {
			list.add(new Point(pieceLine + 1, pieceCol));
		}

		if (pieceLine + 1 <= matrix.length - 1 && pieceCol + 1 <= matrix.length - 1
				&& (matrix[pieceLine + 1][pieceCol + 1] != -1)
				&& Utils.getPieceColor(matrix[pieceLine + 1][pieceCol + 1]) != pieceColor) {
			list.add(new Point(pieceLine + 1, pieceCol + 1));
		}

		if (pieceCol - 1 >= 0 && (pieceLine + 1 <= matrix.length - 1)
				&& (pieceCol - 1 >= 0)
				&& (matrix[pieceLine + 1][pieceCol - 1] != -1) ){
				if (Utils.getPieceColor(matrix[pieceLine + 1][pieceCol - 1]) != pieceColor) {
					list.add(new Point(pieceLine + 1, pieceCol - 1));
				}
		}
	}

}
